package com.hs.hlw.credit.system.common.enumeration;

/**
 * FormFieldInputType
 *
 * @author zhb
 * @date 2018/11/20
 */
public enum FieldDataType_DM {
    CHAR("CHAR"),
    VARCHAR("VARCHAR"),
    VARCHAR2("VARCHAR2"),
    TEXT("TEXT"),
    CLOB("CLOB"),

    DATE("DATE"),
    TIMESTAMP("TIMESTAMP"),
    DATETIME("DATETIME"),

    BIGINT("BIGINT"),
    DECIMAL("DECIMAL"),
    INT("INT"),
    TINYINT("TINYINT"),
    INTEGER("INTEGER"),
    NUMBER("NUMBER"),
    DOUBLE("DOUBLE"),
    FLOAT("FLOAT");

    public String code;

    FieldDataType_DM(String code) {
        this.code = code;
    }

    /**
     * 判断是否为有效值(主要对数字进行判断)
     * @param dataType
     * @param value
     * @return
     */
    public static boolean isValidNumber(String dataType, String value) {
        FieldDataType_DM fieldDataType = getTypeByCode(dataType);
        if (fieldDataType != null) {
            try {
                switch (fieldDataType) {
                    case BIGINT:
                        Long.parseLong(value);
                        break;
                    case INT:
                    case INTEGER:
                        Integer.parseInt(value);
                        break;
                    case TINYINT:
                        Byte.parseByte(value);
                        break;
                    case DOUBLE:
                    case NUMBER:
                        Double.parseDouble(value);
                        break;
                    case FLOAT:
                        Float.parseFloat(value);
                        break;
                    default:
                        break;
                }
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }

    public static FieldDataType_DM getTypeByCode(String code) {
        for (FieldDataType_DM enu : FieldDataType_DM.values()) {
            if (enu.code.equalsIgnoreCase(code)) {
                return enu;
            }
        }
        return null;
    }

    /**
     * 判断是否存在
     *
     * @param code
     * @return
     */
    public static boolean contains(String code) {
        for (FieldDataType_DM enu : FieldDataType_DM.values()) {
            if (enu.code.equalsIgnoreCase(code)) {
                return true;
            }
        }
        return false;
    }
}